package cc.bailongma.me.fragment;


import java.io.Serializable;
import java.util.List;

import cc.bailongma.me.MainActivity;
import cc.bailongma.me.R;
import cc.bailongma.me.adapter.ConnectLogListAdapter;
import cc.bailongma.me.entity.Parent;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ConnectFragment extends Fragment implements OnClickListener{
	private List<Parent> parents;
	private ListView lv;
	private RelativeLayout layout_empty;
	private ConnectLogListAdapter connectLogListAdapter;
	public static TextView tv_call_to;
	public ConnectFragment(){
	}

	@SuppressWarnings("unchecked")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Bundle bundler=this.getArguments();
		parents=(List<Parent>) bundler.getSerializable("parents");
		View view= inflater.inflate(R.layout.connectfragment,container,false);
		lv=(ListView) view.findViewById(R.id.listView1);
		layout_empty=(RelativeLayout) view.findViewById(R.id.layout_impty);
		tv_call_to=(TextView) view.findViewById(R.id.tv_call_to);
		
		view.findViewById(R.id.layout_single).setOnClickListener(this);
		view.findViewById(R.id.layout_double).setOnClickListener(this);
		view.findViewById(R.id.tv_add).setOnClickListener(this);
		
		if(parents.size()==0){
			layout_empty.setVisibility(View.VISIBLE);
		}else{
			reflesh();
		}
		return view;
	}

	private void reflesh() {
		connectLogListAdapter=new ConnectLogListAdapter(getActivity(), parents);
		lv.setAdapter(connectLogListAdapter);
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.layout_single:
			showDialog("single");
			break;
		case R.id.layout_double:
			showDialog("double");
			break;
		case R.id.tv_add:
			MainActivity mActivity=(MainActivity) getActivity();
			mActivity.skip();
			break;
		}
		
	}
	
	
	private void showDialog(String str) {
		Parent parent=parents.get(connectLogListAdapter.position);
		View view=LayoutInflater.from(getActivity()).inflate(R.layout.parent_number_dialog, null);
		final AlertDialog parent_number_dialog=new Builder(getActivity()).setView(view).create();
		TextView tv=(TextView) view.findViewById(R.id.tv_description);
		if(str.equals("single")){
			tv.setText("单向通话指令已发送，设备即将拨号给");
		}else if(str.equals("double")){
			tv.setText("双向通话指令已发送，设备即将拨号给");
		}
		view.findViewById(R.id.tv_ok).setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				parent_number_dialog.dismiss();
			}
		});
		parent_number_dialog.show();
		TextView tv_call_to=(TextView) view.findViewById(R.id.tv_call_to);
		tv_call_to.setText(parent.getChengwei()+"("+parent.getPhone()+")");
	}

}
